 package com.rtx.config.quartz;

 import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.javassist.CtClass;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.rtx.api.annotation.RtxJobAnnotation;
import com.rtx.api.quartz.job.BaseJob;
import com.rtx.common.JavassistHelp;
import com.rtx.wms.std.ctclass.ClassHolder;

 /**
  * 启动时加载quartz定时任务
  * @author ltt
  *
  */
@SuppressWarnings("all")
@Component
public class QuzrtzJobLoad implements CommandLineRunner{

	 Logger log = LoggerFactory.getLogger(QuzrtzJobLoad.class);

	 @Autowired
	 private Scheduler sched;

	 @Autowired
	 private ApplicationContext appContext;

	 @Override
	 public void run(String... args) {
		 String[] beanName = appContext.getBeanNamesForAnnotation(RtxJobAnnotation.class);
		 for(String name:beanName) {
			 try {
				 if(!load(name.toUpperCase())) {
					 continue;
				 }

				 BaseJob job = (BaseJob) appContext.getBean(name);
				 RtxJobAnnotation jobAnnotation = job.getClass().getAnnotation(RtxJobAnnotation.class);
				 String jobName = jobAnnotation.jobName();
				 String description = jobAnnotation.description();
				 String cron = jobAnnotation.cron();
				 String trigger = StringUtils.hasText(jobAnnotation.trigger()) ? jobAnnotation.trigger() : jobName + "Trigger";
				 String group = StringUtils.hasText(jobAnnotation.group()) ? jobAnnotation.group() : "EDI";
				 boolean exists = sched.checkExists(new JobKey(jobName, group));
				 if(exists) {
					 continue;
				 }
				 JobDetail jobDetail = JobBuilder
						 .newJob(job.getClass())
						 .withIdentity(jobName, group)
						 .withDescription(description)
						 .build();
				 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
				 CronTrigger cronTrigger = TriggerBuilder
						 .newTrigger()
						 .withIdentity(trigger, group)
						 .withSchedule(scheduleBuilder)
						 .build();
				 sched.scheduleJob(jobDetail,cronTrigger);
			 } catch (Exception e) {
				 log.error("加载quartz job 异常",e);
			 }
		 }
	 }
	 
		private boolean load(String name) {
			//ApiOrdersToWmsJob,OrdersFinishJob,OrdersReturnJob
			//StorerToWmsJob,SKUToWmsJob
			//ApiReceiptToWmsJob,ReceiptFinishJob,ReceiptReturnJob
			//SKUToWmsJob,ApiOrdersToWmsJob,OrdersFinishJob,ApiReceiptToWmsJob,ReceiptFinishJob
			//ZTKYExpressJob
			List<String> load = Arrays.asList("".toUpperCase().split(","));
			if(load.contains(name)) {
				return true;
			}else {
				return false;
			}
		}
 }
